//全局隐藏验证码
var hideVerifyCode = 0;
$(function () {
    initDatagrid();
    $("#queryButton").click(function () {
        $("#listTable").datagrid('load', {
            type: $("#orderStatus").combobox('getValue'),
            queryStr: $("#queryStr").val(),
            orderTime: $("input[name=orderTime]").val()

        })
    });

    $('#query').click(function () {
        var phone = $("#phone").val();
        var personId = $("#personId").val();
        var expendScoreSMS = $("#expendScoreSMS").val();
        var verifyCode = $("#verifyCode").val();

        if (phone == "" || phone == null) {
            parent.$.messager.show('请输入手机号');
            return false;
        } else {
            if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(phone)) {
                parent.$.messager.show('请正确输入手机号');
                return false;
            }
        }
        if (!personId || personId.length < 6) {
            parent.$.messager.show('请正确输入身份证号后六位');
            return false;
        }
        var url = $.util.getRequestJson('/json/storeHandle.queryUserIntegral.json');
        $.util.get(url, {
            phone: phone,
            personId: personId,
            expendScoreSMS: expendScoreSMS,
            verifyCode: verifyCode
        }, function (res) {
            if (res.success) {
                $('#surplusIntegralTr').show();
                $('#surplusIntegral').text(res.data + '分');
                return;
            }
            $.easyui.messager.show(res.message);
        });
    });

    //短信收积分操作
    isclick = true;
    $("#btn2").click(function () {
        $('#surplusIntegralTr').hide();
        var phone = $("#phone").val();
        var personId = $("#personId").val();
        var expendScoreSMS = $("#expendScoreSMS").val();
        var verifyCode = $("#verifyCode").val();

        if (phone == "" || phone == null) {
            parent.$.messager.show('请输入手机号');
            return false;
        } else {
            if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(phone)) {
                parent.$.messager.show('请正确输入手机号');
                return false;
            }
        }
        if (!personId || personId.length < 6) {
            parent.$.messager.show('请正确输入身份证号后六位');
            return false;
        }
        if (expendScoreSMS == "" || expendScoreSMS == null) {
            parent.$.messager.show('请输入兑换积分');
            return false;
        } else {
            if (!/^\+?[1-9]\d*$/.test(expendScoreSMS)) {
                parent.$.messager.show('请正确输入兑换积分');
                return false;
            }
        }
        if (verifyCode == "" || verifyCode == null) {
            parent.$.messager.show('请输入验证码');
            return false;
        } else {
            if (verifyCode != hideVerifyCode) {
                parent.$.messager.show('输入的验证码有误');
                return false;
            }
        }
        if (!isclick) {
            return;
        }
        isclick = false;
        var url = $.util.getRequestJson('/json/storeHandle.gatherBySMS.json');
        $.util.get(url, {
            phone: phone,
            personId: personId,
            expendScoreSMS: expendScoreSMS,
            verifyCode: verifyCode
        }, function (res) {
            $('#SMS').form("clear");
            if (res.success) {
                $('#listTable').datagrid('reload');
                hideVerifyCode = 0;
            }
            $.easyui.messager.show(res.message);
            isclick = true;
        });
    });


    //实时获取payCode的值并进行判断
    $('#payCode').bind('input propertychange', function () {
        var payCode = $('#payCode').val();
        var before = payCode.substring(0, 1);
        //var last = payCode.substring(payCode.length-2,payCode.length+1);
        //var middle = payCode.substring(2,payCode.length-2);
        if (before == "D" || before == "d") {
            $('#expendScoreNum').attr("style", "display:none;");
        } else {
            $('#expendScoreNum').removeAttr("style");
        }
    });

    $('#payCode').keydown(function (event) {
        if (event.keyCode == 13) {
            var payCode = $('#payCode').val();
            var before = payCode.substring(0, 1);
            if (before == "D" || before == "d") {
                takeScore();
            }
        }
    });
    //扫码收积分操作
    $("#btn1").click(function () {
        $('#surplusIntegralTr').hide();
        takeScore();
    });

    function takeScore() {
        var payCode = $("#payCode").val();
        var before = payCode.substring(0, 1);
        var expendScoreCode = $("#expendScoreCode").val();
        var attribute = $('#expendScoreNum').attr('style');
        if (payCode == "" || payCode == null) {
            parent.$.messager.show('请输入积分支付码/商品兑换码');
            return;
        }
        if (!attribute) {
            if (expendScoreCode == "" || expendScoreCode == null) {
                parent.$.messager.show('请输入兑换积分数量');
                return;
            } else {
                if (!/^\+?[1-9]\d*$/.test(expendScoreCode)) {
                    parent.$.messager.show('请正确输入兑换积分数量');
                    return;
                }
            }
        }
        if (before == "D" || before == "d") {
            var url = $.util.getRequestJson('/json/storeHandle.gatherByOrder.json');
            $.util.get(url, {payCode: payCode}, function (res) {
                $('#code').form("clear");
                if (res.success) {
                    $('#listTable').datagrid('reload');
                }
                $.easyui.messager.show(res.message);

            });
        } else if (before == "Z" || before == "z") {
            if (!isclick) {
                return;
            }
            isclick = false;
            var url = $.util.getRequestJson('/json/storeHandle.gatherByPayCode.json');
            $.util.get(url, {payCode: payCode, expendScoreCode: expendScoreCode}, function (res) {
                $('#code').form("clear");
                if (res.success) {
                    $('#listTable').datagrid('reload');
                }
                $.easyui.messager.show(res.message);
                isclick = true;
            });
        } else {
            parent.$.messager.show('输入的积分支付码/商品兑换码格式不正确');
            return;
        }
    }
});


// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/consumeManage.tradeList.json');

    $('#listTable').datagrid({
        url: url + '?rows=5',
        idField: 'id',
        fit: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        border: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: [
            [
                {
                    field: 'orderNo',
                    title: '订单编号',
                    align: 'center',
                    width: 15,
                    formatter: function (value, row) {
                        return value == null ? "——" : value;
                    }
                },
                {
                    field: 'phone',
                    title: '客户手机号',
                    align: 'center',
                    width: 12,
                    formatter: function (value, row, index) {
                        return value.toString().substr(0, 3) + "****" + value.toString().substr(7);
                    }
                },
                {
                    field: 'name',
                    title: '客户姓名',
                    align: 'center',
                    width: 12
                },
                {
                    field: 'expendScore',
                    title: '消费积分',
                    align: 'center',
                    width: 8
                },
                {
                    field: 'expendTime',
                    title: '消费时间',
                    align: 'center',
                    width: 12
                }, {
                field: 'id',
                title: '操作',
                align: 'center',
                width: 18,
                formatter: function (value, row) {
                    aEl = '<a href=javascript:gatherScoreDetail("{0}"); class="easyui-tooltip " title="详情">详情</a>';
                    //格式化操作按钮
                    return $.string.format(aEl, row.id);
                }
            }
            ]
        ]
    });
}

// 查看订单详情   收分记录id
function gatherScoreDetail(gatherScoreId) {

    var title = "详情";
    var url = $.util.getRootPath() + '/page/shopManager/gatherScore/gatherScoreDetail.html';
    url = url + '?id=' + gatherScoreId;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 550,
        height: 370,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton: false,
        closeButtonText: '关闭'
    });
}

//获取验证码倒计时
var countdown = 60;

//发送验证码

function getVerificationCode() {
    $('#surplusIntegralTr').hide();
    var phone = $("#phone").val();
    var personId = $("#personId").val();
    var expendScoreSMS = $("#expendScoreSMS").val();
    if (phone == "" || phone == null) {
        parent.$.messager.show('请输入手机号');
        return;
    } else {
        if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(phone)) {
            parent.$.messager.show('请正确输入手机号');
            return;
        }
    }
    if (!personId || personId.length < 6) {
        parent.$.messager.show('请正确输入身份证号后六位');
        return;
    }
    if (expendScoreSMS == "" || expendScoreSMS == null) {
        parent.$.messager.show('请输入兑换积分');
        return;
    } else {
        if (!/^\+?[1-9]\d*$/.test(expendScoreSMS)) {
            parent.$.messager.show('请正确输入兑换积分');
            return;
        }
    }
    var url = $.util.getRequestJson('/json/storeHandle.getYzCode.json');
    $.util.get(url, {account: expendScoreSMS, phone: phone, personId: personId, type: 3}, function (data) {
        if (data.success) {
            var isDebug = data.data.isDebug;
            if (isDebug == "true") {
                data.data.verifyCode = data.data.Num;
            }
            hideVerifyCode = data.data.Num;
            $('#SMS').form('load', data.data);
        } else {
            top.$.easyui.messager.show(data.message);
        }
    });
    //获取验证码之前进行判断
    //new invokeSettime("#sendCheackNum");

}

function invokeSettime(obj) {
    var idtime = setInterval(function () {
        settime(obj)
    }, 1000);

    function settime(obj) {
        var el = $(obj);
        if (countdown == 0) {
            el.linkbutton('disable');
            el.find('.l-btn-text').text('发送验证码');
            countdown = 60;
            clearInterval(idtime);
        } else {
            el.linkbutton('enable');
            el.find('.l-btn-text').text("(" + countdown + ")秒后重新获取");
            countdown--;
        }
    }
}
